<?php

session_start();
include 'connect.php';

if(isset($_POST['link_data'])) {  
    $property_id = $_POST['link_data'];
    $user_id = $_SESSION['user_id'];

    $result = mysqli_query($conn, "SELECT property_id FROM wishlists WHERE property_id = ".$property_id." AND user_id = ".$user_id, MYSQLI_STORE_RESULT) or die(mysqli_error($conn));
    $take_id = mysqli_fetch_assoc($result);

    if(isset($take_id['property_id'])){
        mysqli_query($conn, "DELETE FROM wishlists WHERE property_id = ".$property_id." AND user_id = ".$user_id, MYSQLI_STORE_RESULT) or die(mysqli_error($conn)); // If product has already added to wishlist then remove it from wishlist table

        echo 0;
    } 
    else {
        mysqli_query($conn, "INSERT INTO wishlists SET property_id = ".$property_id.", user_id = ".$user_id, MYSQLI_STORE_RESULT) or die(mysqli_error($conn)); // If product has not in wishlist then add to wishlist table

         echo 1;
    }
}

?>
